//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.erp.log;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.UUID;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

//@Aspect
//@Component
//@Order(0)
public class MDCRequestMappingAspect {
    public static final Logger LOGGER = LoggerFactory.getLogger(MDCRequestMappingAspect.class);

    public MDCRequestMappingAspect() {
    }

    @PostConstruct
    public void init() {
        LOGGER.info("MDCRequestMappingAspect init ......");
    }

    private static String get16UUID() {
        UUID id = UUID.randomUUID();
        String[] idd = id.toString().split("-");
        return idd[0] + idd[1] + idd[2];
    }

    private static String getIpAddr(HttpServletRequest request) {
        String ipAddress = request.getHeader("x-forwarded-for");
        if (null == ipAddress || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }

        if (null == ipAddress || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if ("127.0.0.1".equals(ipAddress) || "0:0:0:0:0:0:0:1".equals(ipAddress)) {
                InetAddress inet = null;

                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException var4) {
                    var4.printStackTrace();
                }

                if (null != inet) {
                    ipAddress = inet.getHostAddress();
                }
            }
        }

        if (ipAddress != null && ipAddress.length() > 15 && ipAddress.indexOf(",") > 0) {
            ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
        }

        return ipAddress;
    }

    @Pointcut("(@target(org.springframework.web.bind.annotation.RestController)) && (execution(public * com.example.erp..*.*(..)))")
    public void executionService() {
    }

    @Before("executionService()")
    public void doBefore() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String traceId = request.getHeader("x-trace-id");
        if (StringUtils.isEmpty(traceId)) {
            traceId = get16UUID();
        }

        String uIP = getIpAddr(request);
        MDC.put("traceID", traceId);
        MDC.put("uIP", uIP);
        LOGGER.info("uIP:" + uIP);
    }

    @AfterReturning(pointcut = "executionService()")
    public void doAfterReturning() {
        MDC.clear();
    }
}
